#include"BlockQueue.hpp"
void* Consumer(void* arg)
{
  //消费者读数据
  BlockQueue *bq= (BlockQueue*) arg;
  bq->CLockQueue();
    sleep(1);
  while(true)
  {
    int data = 0;
    bq->pop(data);
    cout<<"Consumer thread ["<<pthread_self()<<"]done data:"<<data<<endl;
  }
    bq->UCLockQueue();
}

void* Product(void* arg)
{
  //生产者生产数据
  BlockQueue *bq= (BlockQueue*) arg;
  
  bq->PLockQueue();
  while(true)
  {  
     int data = random() % 100 + 1;
     bq->push(data);
     cout<<"Product thread [:"<<pthread_self()<<"]done data:"<<data<<endl;
  }
     bq->UPLockQueue();
}

int main()
{
  //创建2个线程

 BlockQueue* bq= new BlockQueue(100);
 pthread_t c,p,c2,c3,p2;

pthread_create(&c,nullptr,Consumer,(void*)bq);
pthread_create(&p,nullptr,Product,(void*)bq);

pthread_create(&c2,nullptr,Product,(void*)bq);
pthread_create(&c3,nullptr,Product,(void*)bq);
pthread_create(&p2,nullptr,Product,(void*)bq);

pthread_join(c,nullptr);
pthread_join(p,nullptr);
pthread_join(c2,nullptr);
pthread_join(c3,nullptr);
pthread_join(p2,nullptr);
delete bq;
  return 0;
}
